//
//  ParticipationDataManager.m
//  app
//
//  Created by 王少宁 on 15/4/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "ParticipationDataManager.h"
#import "HDAppDelegate.h"

@interface ParticipationDataManager ()
@property (nonatomic, retain) JKHttpClient * http;
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) NSMutableArray * mutableArray;
@property (nonatomic,retain) FMDatabaseQueue * fmdbQueue;
@property (nonatomic, retain) NSData * myData;

@property (nonatomic, assign) BOOL includeParticipation;
@property (nonatomic, assign) BOOL includeData;

@end

@implementation ParticipationDataManager

+(ParticipationDataManager *)shareManager
{
    AS_SINGLETON(ParticipationDataManager, manager, {
        manager = [[ParticipationDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
        manager.mutableArray = [[NSMutableArray alloc] init];
        manager.fmdbQueue = [FMDatabaseQueue databaseQueueWithPath:kDBPath];
    })
    return manager;
}

#pragma mark - 发送网络请求
- (void)loadParticipationData:(ParticipationHttpResult *)result flag:(int)flag Id:(int)ID limit:(int)limit cmid:(NSString *)cmid
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    
    NSDictionary * parmas = @{@"flag":[NSString stringWithFormat:@"%d",flag],
                              @"id":[NSString stringWithFormat:@"%d",ID],
                              @"limit":[NSString stringWithFormat:@"%d",limit],
                              @"cmid":cmid};
    
    [self.http get:self.uniteUrl.sbgd_participationUrl params:parmas result:^(JKResponse *resp) {
        DLog(@"%@",resp.request.URL.absoluteString);
        if (resp.OK) {
            
            result.success = YES;
            [[SNBaseInfoManager shareManager] setBaseInfoObject:[NSDate date] forKey:kLastRequestParticiPationDataTime];
            
            if (resp.status == JKResponseOK) {
                
                NSArray * array = resp.content[@"result"];
                
                if (flag == 0 && ID == 0) {
                    [result.dataArray removeAllObjects];
                }
                
                for (NSDictionary * dic in array) {
                    ParticipationModel * model = [ParticipationModel modelWithDict:dic];
                    [result.dataArray addObject:model];
                }
                
                if (array.count < limit) {
                    result.noMoreData = YES;
                } else {
                    result.noMoreData = NO;
                }
                                
            } else if (resp.status == JKResponseTail) {
                if (ID==0 && limit==0) {
                    result.noData = YES;
                } else {
                    result.noMoreData = YES;
                }
            }
        } else if (resp.failure) {
            result.failure = YES;
        }
        [result invoke];
    }];
}

@end
